package org.ccay.personalized;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import org.ccay.core.util.CollectionUtil;
import org.ccay.core.util.StringUtil;

/**
 * 个性化使用  显示使用
 * Created：2013-3-18
 * @author chaowangbang
 * @version:
 */
public class CommonSettingVO {
	
	/**
	 * name
	 */
	private String name;
	
	/**
	 * 显示名称
	 */
	private String displayName;
	
	/**
	 * 对应的value
	 */
	private String value;
	
	/**
	 * 对应的状态
	 */
	
	private Boolean enabled = false;
	
	/**
	 * 存在的子项
	 */
	private List<CommonSettingVO> datas = new ArrayList<CommonSettingVO>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}


	public List<CommonSettingVO> getDatas() {
		return datas;
	}

	public void setDatas(List<CommonSettingVO> datas) {
		this.datas = datas;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}
	/**
	 * 对子项根据传递的参数进行排序
	 * @param mode
	 * @param beanName
	 * Created：2013-3-24
	 * @author chaowangbang
	 */
	public void compareDatas(String mode,String ... beanName){
		ComparatorDatas  comparator = new ComparatorDatas(mode,beanName);
		Collections.sort(this.datas, comparator);
	}
	/**
	 * 内部实现排序的规则
	 * Created：2013-3-24
	 * @author chaowangbang
	 * @version:
	 */
	private class ComparatorDatas implements Comparator{
		private String mode;
		private String[] beanName;
		/**
		 * 设置排序顺序 和参数
		 * @param mode
		 * @param beanName
		 * Created：2013-3-18
		 * @author chaowangbang
		 */
		public ComparatorDatas(String mode,String ... beanName){
			this.mode = mode;
			this.beanName = beanName;
		} 
		/**
		 * 实现排序逻辑
		 */
		public int compare(Object setting1, Object setting2) {
			String val1 = null;
			String val2 = null;
			if(CollectionUtil.isNullOrEmpty(beanName)){
				return 0;
			}
			for(int index=0;index<beanName.length;index++){
				  val1 = this.getPropertyValue(setting1, beanName[index]);
				  val2 = this.getPropertyValue(setting2, beanName[index]);
				  if(0==this.returnFlag(val1,val2)){
					  return 0;
				  }
			}
			return 0;
		}
		/**
		 * value取值
		 * @param ojb
		 * @param name
		 * @return
		 * Created：2013-4-28
		 * @author chaowangbang
		 */
		private String getPropertyValue(Object ojb,String name){
			String value = "";
			try {
				value =  BeanUtils.getProperty(ojb,name);
				if(StringUtil.isNullOrEmpty(value)){
					value = "";  
				}
				return value;
			} catch (IllegalAccessException e) {
				return value;
			} catch (InvocationTargetException e) {
				return value;
			} catch (NoSuchMethodException e) {
				return value;
			}
		}
		/**
		 * value比较大小
		 * @param val1
		 * @param val2
		 * @return
		 * Created：2013-3-24
		 * @author chaowangbang
		 */
		private int returnFlag(String val1,String val2){
			int flag = val1.compareTo(val2);
			if(flag==0){
				return 0;
			}
			if("asc".equalsIgnoreCase(mode)){
				return flag;
			}
			return -(flag);
			
		} 
		
		
	}
	
}
